<div class="buttons">
  <div class="">
    {#<input type="button" value="{{ button_confirm }}" id="button-confirm" data-loading-text="{{ text_loading }}" class="btn btn-primary " />#}
	<a href="/query.html"><button class="btn btn-primary pull-left">查詢訂單</button></a>

    <input type="button" value="立即下單" id="button-confirm" data-loading-text="{{ text_loading }}" class="btn btn-danger pull-right" />
  </div>
</div>

{#底部下單按鈕#}
<div id="foot">
  <div class="to-top" id="goTop">
    <i class="fa fa-angle-up"></i>
    <br>
    Top
  </div>
  <a href="/query.html"><div class="order-query">查詢訂單</div></a>
  <div id="button-to-buy" class="to-buy">立即下單</div>
</div>
<script type="text/javascript">
  $('#button-to-buy').on('click',function () {
            
			if(document.all) {
				document.getElementById("button-confirm").click();
			}else{
				var e = document.createEvent("MouseEvents");
				e.initEvent("click", true, true);
				document.getElementById("button-confirm").dispatchEvent(e);
			}
   })
  
</script>

<script type="text/javascript"><!--
$('#button-confirm').on('click', function() {
	    
	guestsavehebing();
});

function cartadd(){
    $.ajax({
		url: 'index.php?route=checkout/cart/add',
		type: 'post',
		data: $('#product input[type=\'text\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\']:checked, #product select, #product textarea'),
		dataType: 'json',
		beforeSend: function() {
			//$("#button-confirm-mobile").attr('disabled',true);
			$("#button-confirm").attr('disabled',true);
		},
		complete: function() {
			//$("#button-confirm-mobile").attr('disabled',false);
			
		},
		success: function(json) {
			$('.alert-dismissible, .text-danger').remove();
			$('.form-group').removeClass('has-error');

			if (json['error']) {
				if (json['error']['option']) {
				    var elementnoteop='';
					for (i in json['error']['option']) {
						var element = $('#input-option' + i.replace('_', '-'));
                        var elements = $('#input-options' + i.replace('_', '-'));
                        if(!elementnoteop){
						   elementnoteop=element;
						   if (element.parent().hasClass('input-group')) {
						        if ($(element).parent().children().hasClass('text-danger')) {
								 $('.text-danger').remove();
								}
								element.parent().after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
								window.location.hash = "#"+elements.attr("id");
							} else {
							    if ($(element).parent().children().hasClass('text-danger')) {
								 $('.text-danger').remove();
								}
								element.after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
								window.location.hash = "#"+elements.attr("id");
							}
							
						   
						}
						
						
						
						
						
					}
					
					$("#button-confirm").attr('disabled',false);
				}

				if (json['error']['recurring']) {
					$('select[name=\'recurring_id\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>');
				}

				// Highlight any found errors
				$('.text-danger').parent().addClass('has-error');
			}

			if (json['success']) {
			
			     confirmhe();
				

				//$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');

				//$('html, body').animate({ scrollTop: 0 }, 'slow');

				//$('#cart > ul').load('index.php?route=common/cart/info ul li');
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});


}
function confirmhe(){
 $.ajax({
		url: 'index.php?route=checkout/confirm_he',
		dataType: 'html',
	    beforeSend: function() {
			//$("#button-confirm-mobile").attr('disabled',true);
			$("#button-confirm").attr('disabled',true);
		},
		complete: function() {
			//$("#button-confirm-mobile").attr('disabled',false);
			
		},
		success: function(html) {
			//$('#collapse-checkout-confirm .panel-body').html(html);
			
			//$('#button-confirm').on('click', function() {
				$.ajax({
					url: 'index.php?route=extension/payment/cod/confirm',
					dataType: 'json',
					beforeSend: function() {
						//$("#button-confirm-mobile").attr('disabled',true);
						$("#button-confirm").attr('disabled',true);
					},
					
					success: function(json) {
						if (json['redirect']) {
							location = json['redirect'];	
						}
						//$("#button-confirm").attr('disabled',false);
					},
					error: function(xhr, ajaxOptions, thrownError) {
						alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			//});

		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			$("#button-confirm").attr('disabled',false);
		}
	});
	///////
}
function guestsavehebing(){
	$.ajax({
		url: 'index.php?route=checkout/guest/savehebing',
		type: 'post',
		data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select'),
		dataType: 'json',
		beforeSend: function() {
			//$("#button-confirm-mobile").attr('disabled',true);
			$("#button-confirm").attr('disabled',true);
		},
		complete: function() {
			//$("#button-confirm-mobile").attr('disabled',false);
			
		},
		success: function(json) {
			
			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				var elementnoteo='';
				for (i in json['error']) {
					var element = $('#input-payment-' + i.replace('_', '-'));
					if(!elementnoteo){
					    elementnoteo=element;
					   
					   
					    if ($(element).parent().hasClass('input-group')) {
					        if ($(element).parent().children().hasClass('text-danger')) {
						     $('.text-danger').remove();
							}
							$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
						} else {
						    if ($(element).parent().children().hasClass('text-danger')) {
						     $('.text-danger').remove();
							}
							$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
						} 
					   
					}
					
					
					
				}
				//console.log(element.attr("id"));
				window.location.hash = "#"+elementnoteo.attr("id");
				$("#button-confirm").attr('disabled',false);
				// Highlight any found errors
				$('.text-danger').parent().addClass('has-error');
			} else {
				
				
				cartadd();	
				 
				
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});

}

 // 返回顶部
    $("#goTop").on("click", function () {
        //设置定时器
        var timer = setInterval(function(){
            //获取滚动条高度
            var osTop = $(document).scrollTop();
            //ispeed 最后等于0
            var ispeed = Math.floor(osTop/5);
            //实现滚动条每次-100px；直到0;
            $(document).scrollTop(osTop-ispeed);
            //到0px后清除浮动
            //这里osTop==(osTop-ispeed)即ispeed等于0时 条件成立
            if(osTop==(osTop-ispeed)){
                clearInterval(timer);
            }
        },30);
    });


//--></script>
